sqrt( (z - za)^2 + ( sqrt((x)^2 + (y)^2) - ra)^2 );

Percentage Accurate: 97.7% → 100.0%
Time: 2.2s
Alternatives: 1
Speedup: 3.0×

Specification

?
\[\left(\left(\left(\left(-1000000000 \leq z \land z \leq 1000000000\right) \land \left(-1000000000 \leq za \land za \leq 1000000000\right)\right) \land \left(-1000000000 \leq x \land x \leq 1000000000\right)\right) \land \left(-1000000000 \leq y \land y \leq 1000000000\right)\right) \land \left(-1000000000 \leq ra \land ra \leq 1000000000\right)\]
\[\begin{array}{l} \\ \sqrt{{\left(z - za\right)}^{2} + {\left(\sqrt{{x}^{2} + {y}^{2}} - ra\right)}^{2}} \end{array} \]
(FPCore (z za x y ra)
 :precision binary64
 (sqrt
  (+ (pow (- z za) 2.0) (pow (- (sqrt (+ (pow x 2.0) (pow y 2.0))) ra) 2.0))))
double code(double z, double za, double x, double y, double ra) {
	return sqrt((pow((z - za), 2.0) + pow((sqrt((pow(x, 2.0) + pow(y, 2.0))) - ra), 2.0)));
}
real(8) function code(z, za, x, y, ra)
    real(8), intent (in) :: z
    real(8), intent (in) :: za
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: ra
    code = sqrt((((z - za) ** 2.0d0) + ((sqrt(((x ** 2.0d0) + (y ** 2.0d0))) - ra) ** 2.0d0)))
end function
public static double code(double z, double za, double x, double y, double ra) {
	return Math.sqrt((Math.pow((z - za), 2.0) + Math.pow((Math.sqrt((Math.pow(x, 2.0) + Math.pow(y, 2.0))) - ra), 2.0)));
}
def code(z, za, x, y, ra):
	return math.sqrt((math.pow((z - za), 2.0) + math.pow((math.sqrt((math.pow(x, 2.0) + math.pow(y, 2.0))) - ra), 2.0)))
function code(z, za, x, y, ra)
	return sqrt(Float64((Float64(z - za) ^ 2.0) + (Float64(sqrt(Float64((x ^ 2.0) + (y ^ 2.0))) - ra) ^ 2.0)))
end
function tmp = code(z, za, x, y, ra)
	tmp = sqrt((((z - za) ^ 2.0) + ((sqrt(((x ^ 2.0) + (y ^ 2.0))) - ra) ^ 2.0)));
end
code[z_, za_, x_, y_, ra_] := N[Sqrt[N[(N[Power[N[(z - za), $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[(N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] + N[Power[y, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - ra), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{{\left(z - za\right)}^{2} + {\left(\sqrt{{x}^{2} + {y}^{2}} - ra\right)}^{2}}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 1 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 97.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{{\left(z - za\right)}^{2} + {\left(\sqrt{{x}^{2} + {y}^{2}} - ra\right)}^{2}} \end{array} \]
(FPCore (z za x y ra)
 :precision binary64
 (sqrt
  (+ (pow (- z za) 2.0) (pow (- (sqrt (+ (pow x 2.0) (pow y 2.0))) ra) 2.0))))
double code(double z, double za, double x, double y, double ra) {
	return sqrt((pow((z - za), 2.0) + pow((sqrt((pow(x, 2.0) + pow(y, 2.0))) - ra), 2.0)));
}
real(8) function code(z, za, x, y, ra)
    real(8), intent (in) :: z
    real(8), intent (in) :: za
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: ra
    code = sqrt((((z - za) ** 2.0d0) + ((sqrt(((x ** 2.0d0) + (y ** 2.0d0))) - ra) ** 2.0d0)))
end function
public static double code(double z, double za, double x, double y, double ra) {
	return Math.sqrt((Math.pow((z - za), 2.0) + Math.pow((Math.sqrt((Math.pow(x, 2.0) + Math.pow(y, 2.0))) - ra), 2.0)));
}
def code(z, za, x, y, ra):
	return math.sqrt((math.pow((z - za), 2.0) + math.pow((math.sqrt((math.pow(x, 2.0) + math.pow(y, 2.0))) - ra), 2.0)))
function code(z, za, x, y, ra)
	return sqrt(Float64((Float64(z - za) ^ 2.0) + (Float64(sqrt(Float64((x ^ 2.0) + (y ^ 2.0))) - ra) ^ 2.0)))
end
function tmp = code(z, za, x, y, ra)
	tmp = sqrt((((z - za) ^ 2.0) + ((sqrt(((x ^ 2.0) + (y ^ 2.0))) - ra) ^ 2.0)));
end
code[z_, za_, x_, y_, ra_] := N[Sqrt[N[(N[Power[N[(z - za), $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[(N[Sqrt[N[(N[Power[x, 2.0], $MachinePrecision] + N[Power[y, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - ra), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\sqrt{{\left(z - za\right)}^{2} + {\left(\sqrt{{x}^{2} + {y}^{2}} - ra\right)}^{2}}
\end{array}

Alternative 1: 100.0% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \mathsf{hypot}\left(z - za, \mathsf{hypot}\left(x, y\right) - ra\right) \end{array} \]
(FPCore (z za x y ra) :precision binary64 (hypot (- z za) (- (hypot x y) ra)))
double code(double z, double za, double x, double y, double ra) {
	return hypot((z - za), (hypot(x, y) - ra));
}
public static double code(double z, double za, double x, double y, double ra) {
	return Math.hypot((z - za), (Math.hypot(x, y) - ra));
}
def code(z, za, x, y, ra):
	return math.hypot((z - za), (math.hypot(x, y) - ra))
function code(z, za, x, y, ra)
	return hypot(Float64(z - za), Float64(hypot(x, y) - ra))
end
function tmp = code(z, za, x, y, ra)
	tmp = hypot((z - za), (hypot(x, y) - ra));
end
code[z_, za_, x_, y_, ra_] := N[Sqrt[N[(z - za), $MachinePrecision] ^ 2 + N[(N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision] - ra), $MachinePrecision] ^ 2], $MachinePrecision]
\begin{array}{l}

\\
\mathsf{hypot}\left(z - za, \mathsf{hypot}\left(x, y\right) - ra\right)
\end{array}
Derivation
  1. Initial program 97.3%

    \[\sqrt{{\left(z - za\right)}^{2} + {\left(\sqrt{{x}^{2} + {y}^{2}} - ra\right)}^{2}} \]
  2. Step-by-step derivation
    1. unpow297.3%

      \[\leadsto \sqrt{\color{blue}{\left(z - za\right) \cdot \left(z - za\right)} + {\left(\sqrt{{x}^{2} + {y}^{2}} - ra\right)}^{2}} \]
    2. unpow297.3%

      \[\leadsto \sqrt{\left(z - za\right) \cdot \left(z - za\right) + \color{blue}{\left(\sqrt{{x}^{2} + {y}^{2}} - ra\right) \cdot \left(\sqrt{{x}^{2} + {y}^{2}} - ra\right)}} \]
    3. hypot-def98.1%

      \[\leadsto \color{blue}{\mathsf{hypot}\left(z - za, \sqrt{{x}^{2} + {y}^{2}} - ra\right)} \]
    4. unpow298.1%

      \[\leadsto \mathsf{hypot}\left(z - za, \sqrt{\color{blue}{x \cdot x} + {y}^{2}} - ra\right) \]
    5. unpow298.1%

      \[\leadsto \mathsf{hypot}\left(z - za, \sqrt{x \cdot x + \color{blue}{y \cdot y}} - ra\right) \]
    6. hypot-def100.0%

      \[\leadsto \mathsf{hypot}\left(z - za, \color{blue}{\mathsf{hypot}\left(x, y\right)} - ra\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\mathsf{hypot}\left(z - za, \mathsf{hypot}\left(x, y\right) - ra\right)} \]
  4. Final simplification100.0%

    \[\leadsto \mathsf{hypot}\left(z - za, \mathsf{hypot}\left(x, y\right) - ra\right) \]

Reproduce

?
herbie shell --seed 1 
(FPCore (z za x y ra)
  :name "sqrt( (z - za)^2 + ( sqrt((x)^2 + (y)^2) - ra)^2 );"
  :precision binary64
  :pre (and (and (and (and (and (<= -1000000000.0 z) (<= z 1000000000.0)) (and (<= -1000000000.0 za) (<= za 1000000000.0))) (and (<= -1000000000.0 x) (<= x 1000000000.0))) (and (<= -1000000000.0 y) (<= y 1000000000.0))) (and (<= -1000000000.0 ra) (<= ra 1000000000.0)))
  (sqrt (+ (pow (- z za) 2.0) (pow (- (sqrt (+ (pow x 2.0) (pow y 2.0))) ra) 2.0))))